﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string provider = Membership.Provider.Name;

        if (!String.IsNullOrEmpty(provider))
        {
            // Use the default membership provider in web.config if not set separately
            if (String.IsNullOrEmpty(PasswordRecovery1.MembershipProvider))
            {
                PasswordRecovery1.MembershipProvider = provider;
            }
            if (String.IsNullOrEmpty(ChangePassword1.MembershipProvider))
                ChangePassword1.MembershipProvider = provider;
        }

        // Show elements depending on whether initial load or after password change/recovery
        string mode = "login";

        if (!String.IsNullOrEmpty(Request.QueryString["mode"]))
            mode = Request.QueryString["mode"];
        else if (ViewState["mode"] != null)
            mode = (string)ViewState["mode"];
        if (!Page.IsPostBack)
        {
            // if user is authenticated but a return URL set, then assume user is not authorize
            if (User.Identity.IsAuthenticated && Request.QueryString["ReturnUrl"] != null)
            {
                NotAuthorizedLabel.Text = String.Concat(User.Identity.Name,
                    " is not authorized to access the site. Please log in with an authorized account.");
                NotAuthorizedLabel.Visible = true;
            }
        }
        else
            NotAuthorizedLabel.Visible = false;

        setMode(mode);
    }

    protected void btnLostPassword_Click(object sender, EventArgs e)
    {
        setMode("recoverPassword");
    }

    //protected void btnChangePassword_Click(object sender, EventArgs e)
    //{
    //    setMode("changePassword");
    //}

    private void setMode(string mode)
    {
        // hide everything
        Login1.Visible = false;
        LostPasswordButton.Visible = false;
        ChangePasswordButton.Visible = false;
        LinksSeparatorLabel.Visible = false;
        PasswordRecoveryLabel.Visible = false;
        PasswordChangeLabel.Visible = false;
        PasswordRecovery1.Visible = false;
        ChangePassword1.Visible = false;

        switch (mode)
        {
            case "login":
                setLoginVisible(true);
                break;

            case "pwdRecov":
                setLoginVisible(true);
                PasswordRecoveryLabel.Visible = true;
                break;

            case "recoverPassword":
                PasswordRecovery1.Visible = true;
                // use ViewState flag to keep password-recovery control visible since
                //  querystring flag gets cleared on 2nd step of recovery
                ViewState["mode"] = "recoverPassword";
                break;

            case "changePassword":
                ChangePassword1.Visible = true;
                ViewState["mode"] = "changePassword";
                break;
        }
    }


    private void setLoginVisible(bool visible)
    {
        Login1.Visible = visible;
        // Get the web.config for the web application
        System.Configuration.Configuration config =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
        // Get the web.config's <membership> section (will return a valid section, even
        //  if <membership> not actually added to the web.config)
        System.Web.Configuration.MembershipSection membSection =
            (System.Web.Configuration.MembershipSection)config.GetSection("system.web/membership");
        // See whether membership section has a provider element
        foreach (System.Configuration.ProviderSettings provider in membSection.Providers)
        {
            if (provider.ElementInformation.IsPresent)
            {
                // Local web.config has a membership provider - show password-change links
                // Test whether password reset/retrieval supported
                if (Membership.Provider.EnablePasswordReset || Membership.Provider.EnablePasswordRetrieval)
                {
                    // See whether smtp server is set
                    System.Net.Configuration.SmtpSection mailSection =
                        config.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection;

                    if (!String.IsNullOrEmpty(ChangePassword1.MembershipProvider))
                        ChangePasswordButton.Visible = visible;
                    if (mailSection != null)
                    {
                        if (!String.IsNullOrEmpty(PasswordRecovery1.MembershipProvider)
                            && (Membership.Provider.EnablePasswordReset || Membership.Provider.EnablePasswordRetrieval)
                            && mailSection.Network.Host != null)
                            LostPasswordButton.Visible = visible;
                    }
                    if (ChangePasswordButton.Visible && LostPasswordButton.Visible)
                        LinksSeparatorLabel.Visible = true;
                }
            }
        }
    }





    protected void Login1_LoginError(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser(Login1.UserName);
        // Check user exists; if so, was failure due to locked out?
        if (user != null)
        {
            if (user.IsLockedOut)
            {
                Login1.FailureText = String.Format(System.Globalization.CultureInfo.InvariantCulture, "The account {0} has been locked out. Please contact your system administrator.",
                    Login1.UserName);
            }

        }
    }
}
